import router from '@/router'
import setting from './../settings'
import storage from './storage'
import withOutLoginPath from './withOutLoginPath'

router.beforeEach((to, from, next) => {
  // set page title
  // console.log(to);

  if(to.path === '/') {
    console.log('回首页')
    next();
  } else{
    let token = storage.get(setting.tokenName);
    if (token || to.meta.unLogin) {
      setPageTitle(to.meta.title);
      next();
    } else {
      let path = to.path;
      let isNext = false;
      for (let item of withOutLoginPath) {
        if (path.indexOf(item) === 0) {
          setPageTitle(to.meta.title);
          next();
          isNext = true;
        }
      }
      if (!isNext) {
        next('/');
      }
    }
  }
})

const title = setting.title;
function setPageTitle(pageTitle) {
  let windowTitle = title;
  if (pageTitle) {
    windowTitle = `${title} - ${pageTitle}`
  }
  document.title = windowTitle;
}
